package com.example.chronicdisease.controller;

import com.example.chronicdisease.pojo.Users;
import com.example.chronicdisease.response.ApiRestResponse;
import com.example.chronicdisease.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController  // @Controller  +  @ResponseBody
@RequestMapping("/users")
public class UsersController {
    @Autowired
    UsersService usersService;

    //处理登录请求
    // @RequestBody 表示把前端传过来的json字符串转化成java对象，给到Users
    @RequestMapping("/login")  //所以url请求：  http://localhost:8080/users/login 记住还要参数json字符串
    public Map<String,Object> login(@RequestBody Users users){
        Map map=new HashMap<>();
        map.put("result","ok");

        return null;
    }

    @RequestMapping("/login2")  //所以url请求：  http://localhost:8080/users/login2 记住还要参数json字符串
    public Users login2(@RequestBody Users users){

        return usersService.login(users); //返回两种情况：1.有数据 2.null
    }

    @RequestMapping("/login3")  //所以url请求：  http://localhost:8080/users/login3 记住还要参数json字符串
    public ApiRestResponse<Users> login3(@RequestBody Users users){
        Users user=usersService.login(users);

        return ApiRestResponse.success(user);
    }

    @RequestMapping("/register")  //所以url请求：  http://localhost:8080/users/register 记住还要参数json字符串
    public ApiRestResponse register(@RequestBody Users users){

        return usersService.register(users);

    }

    @RequestMapping("/updatepass")  //所以url请求：  http://localhost:8080/users/updatepass 记住还要参数json字符串
    public ApiRestResponse updatepass(@RequestBody Users users){

        return usersService.updatepass(users);
    }

    @RequestMapping("/getUser")  //所以url请求：  http://localhost:8080/users/getUser 记住还要参数json字符串
    public ApiRestResponse getUser(@RequestBody Users users){

        return ApiRestResponse.success(usersService.getUser(users));
    }

    @RequestMapping("/select")  //所以url请求：  http://localhost:8080/users/login3   记住还要参数json字符串
    public ApiRestResponse select(@RequestBody Users user){
        System.out.println("/select得到的参数"+user);
        Users users=usersService.select(user.getUserId());
        System.out.println("/select"+users);
        return ApiRestResponse.success(usersService.select(user.getUserId()));
//        return  usersService.select(userId);
    }

}
